package com.apotik.legenda.dialog;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;

import com.apotik.legenda.dao.UserDao;
import com.apotik.legenda.model.Shift;
import com.apotik.legenda.model.User;
import com.apotik.legenda.observer.GeneralObserver;
import com.apotik.legenda.util.AppUtil;
import com.apotik.legenda.util.PrinterUtil;

public class ClosePOSDialog extends JDialog implements ActionListener {
	private static final long serialVersionUID = 1L;

	int widthThis = 400;
	int heightThis = 250;
	
	private JTextField usernameField;
	private JPasswordField passField;
	
	private JButton btnSubmit;
	private JButton btnCancel;
	
	private GeneralObserver observer;
	private User userCashier;
	private Shift shift;
	
	public ClosePOSDialog(GeneralObserver observer, User userCashier, Shift shift) {
		this.observer = observer;
		this.userCashier = userCashier;
		this.shift = shift;
		
		setResizable(false);
		setTitle("Login Confirmation");
		setSize(widthThis, heightThis);
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension jInternalFrameSize = this.getSize();
		setLocation((dim.width - jInternalFrameSize.width)/2, 10);
		
		JPanel panel = panel();
		getContentPane().add(panel, BorderLayout.CENTER);
	}
	
	private JPanel panel(){
		JPanel panel = new JPanel();
		panel.setLayout(null);
		
		JLabel loginLbl = new JLabel("Login");
		loginLbl.setFont(new Font("Tahoma", Font.BOLD, 20));
		loginLbl.setBounds(10, 5, 70, 28);
		panel.add(loginLbl);
		
		JLabel descLbl = new JLabel("Masukkan username dan password Anda");
		descLbl.setFont(new Font("Tahoma", Font.PLAIN, 12));
		descLbl.setBounds(10, 30, 230, 18);
		panel.add(descLbl);
		
		JPanel headerPanel = new JPanel();
		headerPanel.setBounds(0, 0, this.getWidth(), 55);
		headerPanel.setBackground(Color.WHITE);
		headerPanel.setForeground(new Color(0, 0, 0, 65));
		headerPanel.setBorder(new MatteBorder(0, 0, 1, 0, (Color) SystemColor.scrollbar));
		panel.add(headerPanel);
		
		JLabel usernameLbl = new JLabel("Username");
		usernameLbl.setFont(new Font("Tahoma", Font.BOLD, 15));
		usernameLbl.setBounds(32, 75, 100, 28);
		panel.add(usernameLbl);
		
		usernameField = new JTextField();
		usernameField.setBounds(155, 75, 203, 28);
		usernameField.setColumns(10);
		panel.add(usernameField);
		
		JLabel passLbl = new JLabel("Password");
		passLbl.setFont(new Font("Tahoma", Font.BOLD, 15));
		passLbl.setBounds(32, 115, 100, 28);
		panel.add(passLbl);
		
		passField = new JPasswordField();
		passField.setBounds(155, 115, 203, 28);
		passField.setColumns(10);
		panel.add(passField);
		
		JPanel formPanel = new JPanel();
		formPanel.setBorder(new LineBorder(new Color(192, 192, 192)));
		formPanel.setBounds(10, 59, 370, 110);
		panel.add(formPanel);
		
		JSeparator separator = new JSeparator();
		separator.setForeground(new Color(192, 192, 192));
		separator.setBackground(new Color(192, 192, 192));
		separator.setBounds(10, 174, 370, 1);
		panel.add(separator);
		
		btnSubmit = new JButton("Submit");
		btnSubmit.setBounds(205, 182, 85, 25);
		btnSubmit.addActionListener(this);
		this.getRootPane().setDefaultButton(btnSubmit);
		panel.add(btnSubmit);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setBounds(294, 182, 85, 23);
		btnCancel.addActionListener(this);
		panel.add(btnCancel);
		
		return panel;
	}

	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		
		if(source==btnSubmit){
			String _username = usernameField.getText();
			String _pass = passField.getText();
			
			if(_username.equalsIgnoreCase("")){
				JOptionPane.showMessageDialog(null, "Silahkan masukkan username Anda", "EMPTY", JOptionPane.ERROR_MESSAGE);
				usernameField.requestFocus();
				return;
			}
			
			if(_pass.equalsIgnoreCase("")){
				JOptionPane.showMessageDialog(null, "Silahkan masukkan password Anda", "EMPTY", JOptionPane.ERROR_MESSAGE);
				passField.requestFocus();
				return;
			}
			

			UserDao loginQuery = new UserDao();
			User user = loginQuery.login(_username, _pass);
			if(user==null){
				JOptionPane.showMessageDialog(null, "User tidak ditemukan", "NOT FOUND", JOptionPane.ERROR_MESSAGE);
				passField.setText("");
				usernameField.setText("");
				return;
			}
			
			
			if(!user.getPassword().equalsIgnoreCase(_pass)){
				JOptionPane.showMessageDialog(null, "Password salah", "Incorrect", JOptionPane.ERROR_MESSAGE);
				passField.setText("");
				return;
			}
			
			if(!user.getRole().getRoleCode().equalsIgnoreCase(AppUtil.ROLE_SUPER_ADMIN) && !user.getRole().getRoleCode().equalsIgnoreCase(AppUtil.ROLE_OWNER)){
				JOptionPane.showMessageDialog(null, "Anda tidak punya hak untuk menutup window POS", "Incorrect", JOptionPane.ERROR_MESSAGE);
				passField.setText("");
				usernameField.setText("");
				return;
			}
			
			
			PrinterUtil.printerLaporanTutupKasir(this.userCashier, user, this.shift);
			
			
			this.setVisible(false);
			HashMap<String, Object> hm = new HashMap<>();
			hm.put("isSuccess", true);
			observer.response(hm);
		}else if(source==btnCancel){
			this.setVisible(false);
		}
	}
}
